package com.tm.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tm.CoreConstants;
import com.tm.ResultEntity;
import com.tm.model.EcMemberInfo;
import com.tm.model.EcStoreManage;
import com.tm.model.SpOrder;
import com.tm.service.EcMemberInfoService;
import com.tm.service.EcStoreManageService;
import com.tm.service.OrderService;

@Controller
@RequestMapping(value = "/memberView")
public class EcMemberViewController {

private Logger logger = Logger.getLogger(EcMemberViewController.class);
	
	@Autowired
	private EcMemberInfoService emiService;
	
	@Autowired
	private OrderService oService;
	
	@Autowired
	private EcStoreManageService esmService;
	
	/**
	 * 跳转至个人中心页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/center", method = RequestMethod.GET)
	public String center(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至个人中心页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}
			
			boolean flag = (boolean) request.getSession().getAttribute("isBind");
			
			if(!flag){
				return "member/bind/bind-mobile";
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			//获取会员信息
			EcMemberInfo member = emiService.getmemberByMemberId(memberId);
			
			model.addAttribute("member", member);
			
			String head = (String) request.getSession().getAttribute("head");
			
			model.addAttribute("head", head);
			
			//判断会员是否为门店代理
			EcStoreManage store = esmService.getEcStoreManageByMemberId(memberId);
			
			model.addAttribute("store", store);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至个人中心页面end-----------------------------------------");

		return "member/center";
		
	}
	
	/**
	 * 跳转至绑定手机页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/bindMobile", method = RequestMethod.GET)
	public String bindMobile(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("-------------------------------跳转至绑定手机页面begin-----------------------------------------");
		if(request.getSession().getAttribute("memberId") == null){
			response.sendRedirect(CoreConstants.SJ_DOMAIN);
		}
		System.out.println("-------------------------------跳转至绑定手机页面end-----------------------------------------");

		return "member/bind/bind-mobile";
		
	}
	
	/**
	 * 跳转至我的收藏页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/collect", method = RequestMethod.GET)
	public String collect(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至我的收藏页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			//获取会员信息
			EcMemberInfo member = emiService.getmemberByMemberId(memberId);
			
			model.addAttribute("member", member);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至我的收藏页面end-----------------------------------------");

		return "member/collect";
		
	}
	
	/**
	 * 跳转至我的分享二维码页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/share", method = RequestMethod.GET)
	public String share(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至我的分享二维码页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			//获取会员信息
			EcStoreManage store = emiService.getStoreByMemberId(memberId);
			
			model.addAttribute("store", store);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至我的分享二维码页面end-----------------------------------------");

		return "member/share";
		
	}
	
	/**
	 * 跳转至地址管理页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/address", method = RequestMethod.GET)
	public String address(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至地址管理页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			//获取会员收货地址
			List<Map<String, Object>> list = emiService.getAddressListByMemberId(memberId);
			
			model.addAttribute("list", list);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至地址管理页面end-----------------------------------------");

		return "member/address/list";
		
	}
	
	/**
	 * 跳转至地址选择页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addressChoose", method = RequestMethod.GET)
	public String addressChoose(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至地址管理页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			//获取会员收货地址
			List<Map<String, Object>> list = emiService.getAddressListByMemberId(memberId);
			
			model.addAttribute("list", list);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至地址管理页面end-----------------------------------------");

		return "member/address/list-choose";
		
	}
	
	/**
	 * 跳转至新增地址页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addAddr", method = RequestMethod.GET)
	public String addAddr(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至新增地址页面begin-----------------------------------------");
		try {
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至新增地址页面end-----------------------------------------");

		return "member/address/add";
		
	}
	
	/**
	 * 跳转至编辑地址页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateAddr", method = RequestMethod.GET)
	public String updateAddr(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至编辑地址页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			Integer addrId = Integer.parseInt(request.getParameter("addrId"));
			
			//获取收货地址详情
			Map<String, Object> addr = emiService.getAddressByAddrId(addrId);
			
			model.addAttribute("addr", addr);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至编辑地址页面end-----------------------------------------");

		return "member/address/update";
		
	}
	
	/**
	 * 跳转至个人收益页面（暂只有个门店【即代理商】有收益【即销售订单金额列表】，普通会员暂无）
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/income", method = RequestMethod.GET)
	public String income(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至个人收益页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			EcMemberInfo member = emiService.getmemberByMemberId(memberId);
			
			model.addAttribute("member", member);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至个人收益页面end-----------------------------------------");

		return "member/income";
		
	}
	
	/**
	 * 跳转至个人积分页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/score", method = RequestMethod.GET)
	public String score(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至个人积分页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			EcMemberInfo member = emiService.getmemberByMemberId(memberId);
			
			model.addAttribute("member", member);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至个人积分页面end-----------------------------------------");

		return "member/score";
		
	}
	
	/**
	 * 跳转至销售统计页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saleCount", method = RequestMethod.GET)
	public String saleCount(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至销售统计页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			Map<String, Object> rem = oService.getSaleCount(memberId);
			
			//获取销售统计信息
			model.addAttribute("saleCnt", rem.get("saleCnt"));
			model.addAttribute("saleMoney", rem.get("saleMoney"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至销售统计页面end-----------------------------------------");

		return "member/sale-count";
		
	}
	
	/**
	 * 跳转至发展用户页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/lowerList", method = RequestMethod.GET)
	public String lowerList(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至发展用户页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至发展用户页面end-----------------------------------------");

		return "member/lower-list";
		
	}
	
	/**
	 * 跳转至签到管理页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/sign", method = RequestMethod.GET)
	public String sign(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至签到管理页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			model.addAttribute("memberId", request.getSession().getAttribute("memberId"));
		    
		    ResultEntity re = emiService.getMemberSign((Integer) request.getSession().getAttribute("memberId"));
		    
		    model.addAttribute("signData", re.getData());
		    
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至签到管理页面end-----------------------------------------");

		return "member/sign";
		
	}
	
}
